package com.jcl.recruit.elasticsearch.api.controller;

import com.jcl.core.resp.ApiResponse;
import com.jcl.recruit.elasticsearch.core.entity.ApiResumeCompleteParam;
import com.jcl.recruit.elasticsearch.core.thread.ResumeCrawlerContext;
import com.jcl.recruit.elasticsearch.resume.model.SearchResumeBasicInfo;
import com.jcl.recruit.elasticsearch.resume.service.ResumeCrawlerService;
import com.jcl.recruit.elasticsearch.resume.vo.ResumeCrawlerExportParam;
import com.jcl.recruit.elasticsearch.server.ResumeApiServer;
import com.jcl.recruit.elasticsearch.thread.ResumeIntoElasticTask;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @ClassName ProducerController

 * @Author yyliu
 * @Date 2018/11/12 17:00
 * @Version 1.0
 **/
@RestController
@RequestMapping("/elastic/crawler")
public class CrawlerController {

    private static final Logger logger = LoggerFactory.getLogger(CrawlerController.class);

    @Autowired
    private ResumeCrawlerService resumeCrawlerService;

    @Autowired
    private ResumeApiServer resumeApiServer;


    @RequestMapping(value = "/resume/download/list", method = {RequestMethod.GET})
    public ApiResponse downloadResumeList(String corpCode, String source, Integer curPage, Integer pageSize) {

        Map<String, Object> dataMap = resumeApiServer.getResumeDownList(corpCode, source, curPage, pageSize);

        return ApiResponse.success(dataMap);
    }


    @RequestMapping(value = "/corp/account", method = {RequestMethod.GET})
    public ApiResponse saveResume(String corpCode, String source, Integer curPage, Integer pageSize) {

        if (StringUtils.isEmpty(source)) {
            return ApiResponse.error(" param 'source' is lose!");
        }
        Map<String, Object> dataMap = resumeApiServer.getCorpAccountList(corpCode, source, curPage, pageSize);

        return ApiResponse.success(dataMap);
    }

    @RequestMapping(value = "/resume/export", method = RequestMethod.POST)
    public ApiResponse resumeExport(@RequestBody ResumeCrawlerExportParam resume) {

        Map<String, String> validate = resumeCrawlerService.validateSearchResume(resume);

        SearchResumeBasicInfo searchResume = resumeCrawlerService.exportSearchResume(resume);

        ResumeCrawlerContext context = new ResumeCrawlerContext(searchResume.getId());

        // 开启线程
        ResumeIntoElasticTask.startResumeIntoElasticThread(context);

        return ApiResponse.success("OK");
    }


    @RequestMapping(value = "/resume/complete", method = RequestMethod.POST)
    public ApiResponse completeResume(@RequestBody ApiResumeCompleteParam entity) {

        if (com.jcl.core.utils.StringUtils.isEmpty(entity.getTenantId())) {
            return ApiResponse.error("param 'tenantId' is lose !");
        }

        logger.info("简历标识 -- {} -- 下载完成 ...", entity.getTenantId());
        resumeApiServer.completeResume(entity);

        return ApiResponse.success("SUCCESS");
    }


    @RequestMapping(value = "/resume/keyword", method = RequestMethod.GET)
    public ApiResponse getKeyword() {
        Map<String, Object> map = new HashMap<>();

        String keys = "\"python\", \"爬虫\", \"运营\", \"客服\", \"市场\", \"销售\", \"行政\", \"HR\", \"go\", " +
                "\"spring\", \"vue\", \"react\", \"前端\", \"android\", \"ios\"";
        String[] kyeslist = keys.replaceAll("\"", "").split(",");
        Random random = new Random();
        String keyword = kyeslist[random.nextInt(14)].trim();
        map.put("corpCode", "JCL0000001");
        map.put("keywords", keyword);

        return ApiResponse.success(map);
    }


}
